#!/usr/bin/env node

const inquirer = require('inquirer')
const path = require('path')
const fs = require('fs')
const ejs = require('ejs')

inquirer.prompt([
  {
    type: 'input',
    name: 'name',
    message: '请输入项目名称?'
  }
])
.then(answers => {
  const tmplDir = path.join(__dirname, 'templates')

  // 目标目录
  const destDir = process.cwd() //根目录

  fs.readdir(tmplDir, (err, files) => {
    if(err) throw err
    files.forEach(file => {
      // 通过模板引擎渲染文件
      ejs.renderFile(path.join(tmplDir, file), answers, (err, result) => {
        if(err) throw err

        fs.writeFileSync(path.join(destDir, file), result)
      })
    })
  })
})